/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.task;

import com.appspot.ittrick.drive.trick.api.DriveAPIService;
import com.appspot.ittrick.drive.trick.controller.MainController;
import com.appspot.ittrick.drive.trick.model.FileModel;
import com.google.api.services.drive.model.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class FileUploadTask extends Service<List<FileModel>> {

    private List<java.io.File> upLoadfileList;
    private String parentId;

    public void run(String parentId, List<java.io.File> upLoadfileList) {
        this.upLoadfileList = upLoadfileList;
        this.parentId = parentId;
        this.restart();
    }

    @Override
    protected Task<List<FileModel>> createTask() {
        return new Task<List<FileModel>>() {

//            @Override
//            protected List<FileModel> call() throws Exception {
//                DriveAPIService driveService = MainController.getInstance().getDriveService();
//
//                List<FileModel> fileModelList = new ArrayList();
//                for (java.io.File localFile : upLoadfileList) {
//                    File remoteFile;
//
//                    java.io.File decodeFile = new java.io.File(URLDecoder.decode(localFile.getPath(), System.getProperty("file.encoding")));
//                    
//                    updateMessage(decodeFile.getName());
//
//                    if (!decodeFile.isFile()) {
//                        remoteFile = driveService.insertFolder(parentId, decodeFile.getName());
//                        readFolder(remoteFile.getId(), decodeFile);
//                    } else {
//                        remoteFile = driveService.uploadFile(parentId, decodeFile);
////                        Drive.Files.Insert request = driveService.getFileInsert(parentId, decodeFile);
////                        request.getMediaHttpUploader().setProgressListener(new CustomProgressListener());
////                        
////                        remoteFile = request.execute();
//                    }
//                    fileModelList.add(new FileModel(remoteFile));
//                }
//
//                return fileModelList;
//            }
            
            @Override
            protected List<FileModel> call() throws Exception {
                DriveAPIService driveService = MainController.getInstance().getDriveService();

                List<FileModel> fileModelList = new ArrayList();
                for (java.io.File localFile : upLoadfileList) {
                    File remoteFile;

                    java.io.File decodeFile = new java.io.File(URLDecoder.decode(localFile.getPath(), System.getProperty("file.encoding")));

                    updateMessage(decodeFile.getName());

                    if (!decodeFile.isFile()) {
                        remoteFile = driveService.insertFolder(parentId, decodeFile.getName());
                        readFolder(remoteFile.getId(), decodeFile);
                    } else {
                        remoteFile = driveService.uploadFile(parentId, decodeFile);
                        
                    }
                    fileModelList.add(new FileModel(remoteFile));
                }

                return fileModelList;
            }

            private void readFolder(String parent, java.io.File dir) throws IOException {
                DriveAPIService driveService = MainController.getInstance().getDriveService();

                java.io.File[] files = dir.listFiles();
                if (files == null) {
                    return;
                }

                for (java.io.File localFile : files) {
                    java.io.File decodeFile = new java.io.File(URLDecoder.decode(localFile.getPath(), System.getProperty("file.encoding")));
                    updateMessage(decodeFile.getName());
                    
                    if (!decodeFile.exists()) {
                        continue;

                    } else if (!decodeFile.isFile()) {
                        File remoteFile = driveService.insertFolder(parent, decodeFile.getName());
                        readFolder(remoteFile.getId(), decodeFile);

                    } else if (decodeFile.isFile()) {
                        driveService.uploadFile(parent, decodeFile);
                    }
                }
            }
        };
    }
}
